/**
 * Created by lx on 2017/10/9.
 */
angular.module('module.table.group')
    .controller('GroupListController', GroupListController);
GroupListController.$inject = ['$state', '$stateParams', '$localStorage', 'TableGroupService', 'groupList', 'TableNumService'];
function GroupListController($state, $stateParams, $localStorage, TableGroupService, groupList, TableNumService) {

    var vm = this;

    if (!$localStorage[environmental + "shopid"]) {
        $localStorage.$reset();
        $state.go('login');
    }

    vm.deleteshow = true;
    vm.disablecheck = true;
    vm.checkDelete = checkDelete;
    vm.openDelete = openDelete;
    vm.submitDelete = submitDelete; // 删除分组
    vm.checkedGroup = checkedGroup; // 选择分组
    vm.getTableList = getTableList; // 获取桌台的列表
    vm.goGroupAdd = goGroupAdd; // 添加分组
    var tableid = $stateParams.tableid;
    var shopid = $localStorage[environmental + "shopid"];
    var tabledetil = {};

    if (groupList == 'undefined') {
        vm.grouplist = [];
    } else {
        vm.grouplist = groupList;
        TableNumService.getTableNum(tableid).$promise.then(function (sccF) {
            tabledetil = sccF;
            for (var i = 0; i < groupList.length; i++) {
                var group = groupList[i];
                group['checked'] = false;
                if (groupList[i].groupid == sccF.groupid) {
                    groupList[i].checked = true;
                }
            }
        }, function (error) {
        })

    }

    if (!tableid) {
        vm.disablecheck = false;
    }

    function openDelete(group) {
        $('#deleteModal').modal('show');
        vm.deletegroup = function deletegroup() {
            TableGroupService.deleteGroup(shopid, group.groupid, function (sucdata) {
                for (var i = 0; i < vm.grouplist.length; i++) {
                    if (sucdata.groupid == vm.grouplist[i].groupid) {
                        vm.grouplist.splice(i, 1);
                    }
                }
            }, function (error) {
                console.log(error);
            })
        }
    }

    function checkDelete() {
        vm.deleteshow = false;
    }

    function submitDelete() {
        vm.deleteshow = true;
    }

    function checkedGroup(group) {
        for (var i in vm.grouplist) {
            vm.grouplist[i].checked = false;
        }
        group.checked = true;
        tabledetil.groupid = group.groupid;
        TableNumService.saveTableDetail(shopid, tabledetil, function (sucData) {
            $state.go('tabledetail', {tableid: tableid});
        }, function (errData) {
        })
    }

    function getTableList(group) {
        TableNumService.getTableList(shopid, group.groupid).$promise.then(function (sucD) {
            group.tablelist = sucD;
            group.tablelen = sucD.length;
            if (sucD.length == 0) {
                group.tablelist = [
                    {tablename: '暂无桌台'}
                ];
            }
        }, function (errF) {
            group.tablelist = [
                {tablename: '暂无桌台'}
            ];
            group.tablelen = 0;
        })
    }

    function goGroupAdd() {
        if (tableid) {
            $state.go("groupadd", {tableid: tableid});
        } else {
            console.log(tableid);
            $state.go("groupadd");
        }
    }

}